﻿using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
    /// 数据访问类:evidence_notify
    /// </summary>
    public partial class evidence_notify
    {
        private string databaseprefix = "eazy_"; //数据库表名前缀
        public evidence_notify()
        { }
        #region  BasicMethod ====================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "evidence_notify");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists_I(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "evidence_notify");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.evidence_notify model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "evidence_notify(");
                        strSql.Append("instance_id,code,number,title,xingwei,guiding,address,who,qianm,subtime,add_time)");
                        strSql.Append(" values (");
                        strSql.Append("@instance_id,@code,@number,@title,@xingwei,@guiding,@address,@who,@qianm,@subtime,@add_time)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@code", SqlDbType.NVarChar,20),
                                    new SqlParameter("@number", SqlDbType.NVarChar,20),
                                    new SqlParameter("@title", SqlDbType.NVarChar,255),
                                    new SqlParameter("@xingwei", SqlDbType.NText),
                                    new SqlParameter("@guiding", SqlDbType.NText),
                                    new SqlParameter("@address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@who", SqlDbType.NVarChar,100),
                                    new SqlParameter("@qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@subtime", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime)};
                        parameters[0].Value = model.instance_id;
                        parameters[1].Value = model.code;
                        parameters[2].Value = model.number;
                        parameters[3].Value = model.title;
                        parameters[4].Value = model.xingwei;
                        parameters[5].Value = model.guiding;
                        parameters[6].Value = model.address;
                        parameters[7].Value = model.who;
                        parameters[8].Value = model.qianm;
                        parameters[9].Value = model.subtime;
                        parameters[10].Value = model.add_time;
                        //添加主表数据
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);
                        #endregion

                        #region 添加内容附件====================
                        if (model.attach != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.evidence_notify_attach modelt in model.attach)
                            {
                                strSql4 = new StringBuilder();
                                strSql4.Append("insert into " + databaseprefix + "evidence_notify_attach(");
                                strSql4.Append("article_id,instance_id,sort_id,title,guige,number,add_time)");
                                strSql4.Append(" values (");
                                strSql4.Append("@article_id,@instance_id,@sort_id,@title,@guige,@number,@add_time)");
                                SqlParameter[] parameters4 = {
                                        new SqlParameter("@article_id", SqlDbType.Int,4),
                                        new SqlParameter("@instance_id", SqlDbType.Int,4),
                                        new SqlParameter("@sort_id", SqlDbType.Int,4),
                                        new SqlParameter("@title", SqlDbType.NVarChar,500),
                                        new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                        new SqlParameter("@number", SqlDbType.NVarChar,20),
                                        new SqlParameter("@add_time", SqlDbType.DateTime)};
                                parameters4[0].Value = model.id;
                                parameters4[1].Value = modelt.instance_id;
                                parameters4[2].Value = modelt.sort_id;
                                parameters4[3].Value = modelt.title;
                                parameters4[4].Value = modelt.guige;
                                parameters4[5].Value = modelt.number;
                                parameters4[6].Value = modelt.add_time;
                                DbHelperSQL.GetSingle(conn, trans, strSql4.ToString(), parameters4); //带事务
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.evidence_notify model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update " + databaseprefix + "evidence_notify set ");
                        strSql.Append("instance_id=@instance_id,");
                        strSql.Append("code=@code,");
                        strSql.Append("number=@number,");
                        strSql.Append("title=@title,");
                        strSql.Append("xingwei=@xingwei,");
                        strSql.Append("guiding=@guiding,");
                        strSql.Append("address=@address,");
                        strSql.Append("who=@who,");
                        strSql.Append("qianm=@qianm,");
                        strSql.Append("subtime=@subtime,");
                        strSql.Append("add_time=@add_time");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
                                    new SqlParameter("@instance_id", SqlDbType.Int,4),
                                    new SqlParameter("@code", SqlDbType.NVarChar,20),
                                    new SqlParameter("@number", SqlDbType.NVarChar,20),
                                    new SqlParameter("@title", SqlDbType.NVarChar,255),
                                    new SqlParameter("@xingwei", SqlDbType.NText),
                                    new SqlParameter("@guiding", SqlDbType.NText),
                                    new SqlParameter("@address", SqlDbType.NVarChar,255),
                                    new SqlParameter("@who", SqlDbType.NVarChar,100),
                                    new SqlParameter("@qianm", SqlDbType.NVarChar,20),
                                    new SqlParameter("@subtime", SqlDbType.DateTime),
                                    new SqlParameter("@add_time", SqlDbType.DateTime),
                                    new SqlParameter("@id", SqlDbType.Int,4)};
                        parameters[0].Value = model.instance_id;
                        parameters[1].Value = model.code;
                        parameters[2].Value = model.number;
                        parameters[3].Value = model.title;
                        parameters[4].Value = model.xingwei;
                        parameters[5].Value = model.guiding;
                        parameters[6].Value = model.address;
                        parameters[7].Value = model.who;
                        parameters[8].Value = model.qianm;
                        parameters[9].Value = model.subtime;
                        parameters[10].Value = model.add_time;
                        parameters[11].Value = model.id;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改内容附件==========================
                        //删除已删除的附件
                        new evidence_notify_attach().DeleteList(conn, trans, model.attach, model.id);
                        // 添加/修改附件
                        if (model.attach != null)
                        {
                            StringBuilder strSql4;
                            foreach (Models.evidence_notify_attach modelt in model.attach)
                            {
                                strSql4 = new StringBuilder();
                                if (modelt.id > 0)
                                {
                                    strSql4.Append("update " + databaseprefix + "evidence_notify_attach set ");
                                    strSql4.Append("article_id=@article_id,");
                                    strSql4.Append("instance_id=@instance_id,");
                                    strSql4.Append("sort_id=@sort_id,");
                                    strSql4.Append("title=@title,");
                                    strSql4.Append("guige=@guige,");
                                    strSql4.Append("number=@number");
                                    strSql4.Append(" where id=@id");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@sort_id", SqlDbType.Int,4),
                                            new SqlParameter("@title", SqlDbType.NVarChar,500),
                                            new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                            new SqlParameter("@number", SqlDbType.NVarChar,20),
                                            new SqlParameter("@id", SqlDbType.Int,4)};
                                    parameters4[0].Value = modelt.article_id;
                                    parameters4[1].Value = modelt.instance_id;
                                    parameters4[2].Value = modelt.sort_id;
                                    parameters4[3].Value = modelt.title;
                                    parameters4[4].Value = modelt.guige;
                                    parameters4[5].Value = modelt.number;
                                    parameters4[6].Value = modelt.id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                                else
                                {
                                    strSql4.Append("insert into " + databaseprefix + "evidence_notify_attach(");
                                    strSql4.Append("article_id,instance_id,sort_id,title,guige,number,add_time)");
                                    strSql4.Append(" values (");
                                    strSql4.Append("@article_id,@instance_id,@sort_id,@title,@guige,@number,@add_time)");
                                    SqlParameter[] parameters4 = {
                                            new SqlParameter("@article_id", SqlDbType.Int,4),
                                            new SqlParameter("@instance_id", SqlDbType.Int,4),
                                            new SqlParameter("@sort_id", SqlDbType.Int,4),
                                            new SqlParameter("@title", SqlDbType.NVarChar,500),
                                            new SqlParameter("@guige", SqlDbType.NVarChar,255),
                                            new SqlParameter("@number", SqlDbType.NVarChar,20),
                                            new SqlParameter("@add_time", SqlDbType.DateTime)};
                                    parameters4[0].Value = modelt.article_id;
                                    parameters4[1].Value = modelt.instance_id;
                                    parameters4[2].Value = modelt.sort_id;
                                    parameters4[3].Value = modelt.title;
                                    parameters4[4].Value = modelt.guige;
                                    parameters4[5].Value = modelt.number;
                                    parameters4[6].Value = modelt.add_time;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql4.ToString(), parameters4);
                                }
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //删除主表
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("delete from " + databaseprefix + "evidence_notify ");
            strSql1.Append(" where id=@id ");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters1[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql1.ToString(), parameters1);
            sqllist.Add(cmd);

            //删除附表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from " + databaseprefix + "evidence_notify_attach ");
            strSql2.Append(" where article_id=@article_id ");
            SqlParameter[] parameters2 = {
                    new SqlParameter("@article_id", SqlDbType.Int,4)};
            parameters2[0].Value = id;
            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete_I(int instance_id)
        {
            //删除主表
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("delete from " + databaseprefix + "evidence_notify ");
            strSql1.Append(" where instance_id=@instance_id ");
            SqlParameter[] parameters1 = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters1[0].Value = instance_id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql1.ToString(), parameters1);
            sqllist.Add(cmd);

            //删除附表
            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("delete from " + databaseprefix + "evidence_notify_attach ");
            strSql2.Append(" where instance_id=@instance_id ");
            SqlParameter[] parameters2 = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)};
            parameters2[0].Value = instance_id;
            cmd = new CommandInfo(strSql2.ToString(), parameters2);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_notify GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,code,number,title,xingwei,guiding,address,who,qianm,subtime,add_time ");
            strSql.Append("  from " + databaseprefix + "evidence_notify");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.evidence_notify model = new Models.evidence_notify();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_notify GetModel_I(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,instance_id,code,number,title,xingwei,guiding,address,who,qianm,subtime,add_time ");
            strSql.Append("  from " + databaseprefix + "evidence_notify");
            strSql.Append(" where instance_id=@instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            Models.evidence_notify model = new Models.evidence_notify();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.evidence_notify DataRowToModel(DataRow row)
        {
            Models.evidence_notify model = new Models.evidence_notify();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["instance_id"] != null && row["instance_id"].ToString() != "")
                {
                    model.instance_id = int.Parse(row["instance_id"].ToString());
                }
                if (row["code"] != null)
                {
                    model.code = row["code"].ToString();
                }
                if (row["number"] != null)
                {
                    model.number = row["number"].ToString();
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["xingwei"] != null)
                {
                    model.xingwei = row["xingwei"].ToString();
                }
                if (row["guiding"] != null)
                {
                    model.guiding = row["guiding"].ToString();
                }
                if (row["address"] != null)
                {
                    model.address = row["address"].ToString();
                }
                if (row["who"] != null)
                {
                    model.who = row["who"].ToString();
                }
                if (row["qianm"] != null)
                {
                    model.qianm = row["qianm"].ToString();
                }
                if (row["subtime"] != null && row["subtime"].ToString() != "")
                {
                    model.subtime = DateTime.Parse(row["subtime"].ToString());
                }
                if (row["add_time"] != null && row["add_time"].ToString() != "")
                {
                    model.add_time = DateTime.Parse(row["add_time"].ToString());
                }

                model.attach = new evidence_notify_attach().GetList(model.id);
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,instance_id,code,number,title,xingwei,guiding,address,who,qianm,subtime,add_time ");
            strSql.Append(" FROM " + databaseprefix + "evidence_notify ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,instance_id,code,number,title,xingwei,guiding,address,who,qianm,subtime,add_time ");
            strSql.Append(" FROM " + databaseprefix + "evidence_notify ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion
    }
}
